package kassiopeia.plugin.test.assertions;

import static org.junit.Assert.fail;
import java.io.Serializable;
import kassiopeia.plugin.Event;

public class EventHeaderAssertion {

    private EventAssertion eventAssertion;
    private Event event;
    private String headerName;

    public EventHeaderAssertion(EventAssertion eventAssertion, String headerName) {
        this.eventAssertion = eventAssertion;
        this.event = eventAssertion.getEvent();
        this.headerName = headerName;
    }

    public EventAssertion isEmpty() {
        Object header = event.getHeader(headerName);

        if(header != null) {
            fail("Expected null header, but found: " + header);
        }

        return eventAssertion;
    }

    public EventAssertion isNotEmpty() {
        Object header = event.getHeader(headerName);

        if(header == null) {
            fail("Expected not null header, but found null");
        }

        return eventAssertion;
    }

    public EventAssertion hasType(Class<?> headerType) {
        Object header = event.getHeader(headerName);

        if(header == null) {
            fail("Expected header of type: " + headerType.getCanonicalName() + " but found: null");
        }

        if(!headerType.isInstance(header)) {
            fail("Expected header of type: " + headerType.getCanonicalName() + " but found: " + headerType.getCanonicalName());
        }

        return eventAssertion;
    }

    public EventAssertion isEqualTo(Serializable headerExpected) {
        Serializable header = event.getHeader(headerName);

        if(header == null) {
            fail("Expected header: " + headerExpected + " but found: null");
        }

        if(!headerExpected.equals(header)) {
            fail("Expected header: '" + headerExpected + "', but found: '" + header + "'");
        }

        return eventAssertion;
    }
}
